﻿using ImageProcessor;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfImageProcessTool
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void btnProcess_Click(object sender, RoutedEventArgs e)
        {
            var binPath = System.AppDomain.CurrentDomain.BaseDirectory;

            var fullPath = String.Format("{0}/{1}", binPath, "old");

            DirectoryInfo root = new DirectoryInfo(fullPath);
            foreach (var file in root.GetFiles())
            {

                ReadAndProcessImage(file.FullName, file.Name);

            }

        }


        public void ReadAndProcessImage(String path, String fileName)
        {
            var binPath = System.AppDomain.CurrentDomain.BaseDirectory;
            using (FileStream fileStream = File.OpenRead(path))
            {
                using (ImageFactory imageFactory = new ImageFactory(preserveExifData: true))
                {
                    imageFactory.Load(fileStream);
                    var currentSize = imageFactory.Image.Size;
                    var width = currentSize.Width;
                    var heigth = currentSize.Height;

                    imageFactory.Constrain(new System.Drawing.Size() { Height = width / (heigth * 1024), Width = 1024 })
                                .Save(String.Format("{0}/{1}/{2}", binPath, "new", fileName));
                }
            }



        }
    }
}
